/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import gforgeclient.GForgeAPI;
import gforgeclient.GForgeAPIPortType;
import gforgeclient.GForgeAPI_Impl;
import gforgeclient.Group;
import gforgeclient.ProjectCategory;
import gforgeclient.ProjectTask;

import java.rmi.RemoteException;

import java.util.LinkedList;
import javax.xml.rpc.ServiceException;

/**
 *
 * @author cayo, carolina e wesley
 */
public class GforgeClientmain {
	private String idSession;
        private static LinkedList<String> tasksALL;
        private int next;

        private String allTasks;

        public GforgeClientmain(String login, String pass) {
            try {
                main(login, pass);
            } catch (Exception e) {
                // we don't care
            }
            
        }

	private void main(String login, String password) throws ServiceException, RemoteException{
                tasksALL = new LinkedList<String>();
                allTasks = "";
                next = -1;

                GForgeAPI service = new GForgeAPI_Impl();
		GForgeAPIPortType port = service.getGForgeAPIPort();

		idSession = port.login(login, password );
		int user_id = GForgeConstants.getUser_id(login);
		//System.out.println("Retorno do login: "+ idSession);

               // System.out.println("Lista dos projetos");
		Group[] publicProjects = port.userGetGroups(idSession, user_id);
		for (Group pp2 : publicProjects) {
			//System.out.println("User group:" + pp2.getGroup_name() + "=" + pp2.getGroup_id());

			int group_id = pp2.getGroup_id();
			int group_project_id = GForgeConstants.getProject_group_id(group_id);
                       // System.out.println("group_id=" + group_id + " group_project_id=" + group_project_id);

			ProjectCategory[] publics = port.getProjectTaskCategories(idSession, group_id, group_project_id);
			for (ProjectCategory pp : publics) {
				//System.out.println("   + " + pp.getCategory_name());
                                
                               // System.out.println("Tasks: 1");
				ProjectTask[] tasks = port.getProjectTasks(idSession, group_id, group_project_id, 100, 1, pp.getCategory_id(), -1);
				for(ProjectTask tks : tasks) {
					tasksALL.add(pp2.getGroup_name() + " - "+ pp.getCategory_name() + ": " + tks.getDetails());
                                        allTasks = allTasks + "<p></p><strong>" + pp2.getGroup_name() + "</strong>" + " - "+ pp.getCategory_name() + ": " + tks.getDetails();
				}

			}
                        System.out.println();
		}
                if (tasksALL.size() == 0) {
                    tasksALL.add("No tasks for you");
                }

                if (allTasks.length()== 0) {
                   allTasks = "<strong>No tasks for you :DD</strong>";
                }

		String logout = port.logout(idSession);
		//System.out.println("Logout: " + logout);
		//System.out.println("Funfou...");
	}

	public String getNextTasks() {
            if (next < tasksALL.size() - 1 ) {
                next++;
            } else {
                next = 0;
            }            
            //return tasksALL.get(next);
            return allTasks ;
	}
}

